package com.cy.jxc.service.impl;


import com.cy.jxc.MyException.ServiceExecption;
import com.cy.jxc.dao.UserSettingDao;
import com.cy.jxc.entity.User;
import com.cy.jxc.service.UserSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class UserSettingServiceImpl implements UserSettingService {
    @Autowired
    private UserSettingDao userSettingDao;

    private RedisTemplate redisTemplate;

    @Override
    public User findByUsername(String name) {
        if (name==null)
            throw new ServiceExecption("姓名不能为空");
        User user= userSettingDao.findByUsername(name);
        if (user==null)
            throw new ServiceExecption("用户可能已经不存在！");

        return user;
    }

    @Override
    public Integer UpdateUsername(String newName, String name) {
        Integer rows=0;
        if(newName==null||name==null){
            throw new ServiceExecption("用户名不能为空");
        }else{
            User user = userSettingDao.findByUsername(newName);
            if(user == null){
                rows = userSettingDao.UpdateUsername(newName, name);
            }else {
                throw new ServiceExecption("用户名已存在");
            }
        }
        return rows;
    }

    @Override
    public Integer updatePsw(String password, String name) {

        if (password==null)
            throw new ServiceExecption("密码不能为空");
        Integer rows = userSettingDao.UpdatePsw(password, name);
        return rows;
    }

    @Override
    public Integer updateEmail(String Email, String name) {
        if (Email==null)
            throw new ServiceExecption("邮箱不能为空");
        Integer rows = userSettingDao.UpdateEmail(Email, name);
        return rows;
    }

//    @Override
//    public Integer UpdateByUsername(String name,User user) {
//        if (name==null)
//            throw new ServiceExecption("姓名不能为空");
//        if (user.getPassword()==null)
//            throw new ServiceExecption("密码不能为空");
//
//        Integer rows = userSettingDao.UpdateByUsername(user,name);
//        if (rows==null)
//            throw new ServiceExecption("用户可能已经不存在");
//
//        return rows;
//    }
}
